/*
 * LDIR
 * 
 * LDIR, a GPS cartography application built for Let's Do It Romania eco 
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 * 
 * http://generasoftware.com/
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <avkon.hrh>
#include <avkon.rsg>
#include <eikmenup.h>
#include <aknappui.h>
#include <eikcmobs.h>
#include <barsread.h>
#include <stringloader.h>
#include <gdi.h>
#include <eikedwin.h>
#include <eikenv.h>
#include <eikseced.h>
#include <aknpopupfieldtext.h>
#include <aknnumedwin.h>
#include <eikappui.h>
#include <aknviewappui.h>
#include <LDIR.rsg>
#include "LDIRSettings.h"
#include "Settings.h"
#include "LDIR.hrh"
#include "LDIRWaste.hrh"
#include "LDIRSettings.hrh"
#include "LDIRSettingsView.h"

/**
 * Construct the CLDIRSettings instance
 * @param aCommandObserver command observer
 */
CLDIRSettings::CLDIRSettings( CSettings& aSettings,
	MEikCommandObserver* aCommandObserver ) :
	iSettings( aSettings ), iCommandObserver( aCommandObserver )
	{
	}
/** 
 * Destroy any instance variables
 */
CLDIRSettings::~CLDIRSettings()
	{
	}

/**
 * Handle system notification that the container's size has changed.
 */
void CLDIRSettings::SizeChanged()
	{
	if ( ListBox() )
		{
		ListBox()->SetRect( Rect() );
		}
	}

/**
 * Create one setting item at a time, identified by id.
 * CAknSettingItemList calls this method and takes ownership
 * of the returned value.  The CAknSettingItem object owns
 * a reference to the underlying data, which EditItemL() uses
 * to edit and store the value.
 */
CAknSettingItem* CLDIRSettings::CreateSettingItemL( TInt aId )
	{
	switch ( aId )
		{
		case ELDIRSettingsViewEdit1:
			{
			CAknTextSettingItem* item = new ( ELeave ) CAknTextSettingItem(
				aId, iSettings.Username() );
			return item;
			}
		case ELDIRSettingsViewSecret1:
			{
			CAknPasswordSettingItem* item =
				new ( ELeave ) CAknPasswordSettingItem( aId,
					CAknPasswordSettingItem::EAlpha, iSettings.Password() );
			return item;
			}
		}

	return NULL;
	}

/**
 * Edit the setting item identified by the given id and store
 * the changes into the store.
 * @param aIndex the index of the setting item in SettingItemArray()
 * @param aCalledFromMenu true: a menu item invoked editing, thus
 *    always show the edit page and interactively edit the item;
 *    false: change the item in place if possible, else show the edit page
 */
void CLDIRSettings::EditItemL( TInt aIndex, TBool aCalledFromMenu )
	{
	CAknSettingItem* item = ( *SettingItemArray() )[aIndex];
	switch ( item->Identifier() )
		{
		}

	CAknSettingItemList::EditItemL( aIndex, aCalledFromMenu );

	TBool storeValue = ETrue;
	switch ( item->Identifier() )
		{
		}

	if ( storeValue )
		{
		item->StoreL();
		SaveSettingValuesL();
		}
	}
/**
 *    Handle the "Change" option on the Options menu.  This is an
 *    alternative to the Selection key that forces the settings page
 *    to come up rather than changing the value in place (if possible).
 */
void CLDIRSettings::ChangeSelectedItemL()
	{
	if ( ListBox()->CurrentItemIndex() >= 0 )
		{
		EditItemL( ListBox()->CurrentItemIndex(), ETrue );
		}
	}

/**
 *    Load the initial contents of the setting items.  By default,
 *    the setting items are populated with the default values from
 *     the design.  You can override those values here.
 *    <p>
 *    Note: this call alone does not update the UI.  
 *    LoadSettingsL() must be called afterwards.
 */
void CLDIRSettings::LoadSettingValuesL()
	{
	// load values into iSettings
	}

/**
 *    Save the contents of the setting items.  Note, this is called
 *    whenever an item is changed and stored to the model, so it
 *    may be called multiple times or not at all.
 */
void CLDIRSettings::SaveSettingValuesL()
	{
	// store values from iSettings
	}

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CLDIRSettings::HandleResourceChange( TInt aType )
	{
	CAknSettingItemList::HandleResourceChange( aType );
	SetRect(
		iAvkonViewAppUi->View( TUid::Uid( ELDIRSettingsViewId ) )->ClientRect() );
	}

/** 
 * Handle key event (override)
 * @param aKeyEvent key event
 * @param aType event code
 * @return EKeyWasConsumed if the event was handled, else EKeyWasNotConsumed
 */
TKeyResponse CLDIRSettings::OfferKeyEventL( const TKeyEvent& aKeyEvent,
	TEventCode aType )
	{
	if ( aKeyEvent.iCode == EKeyLeftArrow || aKeyEvent.iCode == EKeyRightArrow )
		{
		// allow the tab control to get the arrow keys
		return EKeyWasNotConsumed;
		}

	return CAknSettingItemList::OfferKeyEventL( aKeyEvent, aType );
	}

